package com.yenlien.traceyun.business.controller;

import com.alibaba.fastjson.JSONObject;
import com.yenlien.traceyun.business.entity.Box;
import com.yenlien.traceyun.business.entity.Subpackage;
import com.yenlien.traceyun.business.interceptor.UserAuthUtil;
import com.yenlien.traceyun.business.service.BaseSoilService;
import com.yenlien.traceyun.business.service.BoxService;
import com.yenlien.traceyun.business.service.ScanRecordService;
import com.yenlien.traceyun.business.service.SubpackageService;
import com.yenlien.traceyun.common.bean.UserInfo;
import com.yenlien.traceyun.webase.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/box")
public class BoxController extends BaseController {

    @Autowired
    private BoxService boxService;

    @Autowired
    private ScanRecordService scanRecordService;

    @Autowired
    private BaseSoilService baseSoilService;

    @Autowired
    private SubpackageService subpackageService;

    /**
     * 查询12月赋码量
     *
     * @return
     */
    @PostMapping("/uniqueId")
    public Object nuniqueId() {
        return success(boxService.uniqueId(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    /**
     * 查询扫码图
     *
     * @return
     */
    @PostMapping("/scan")
    public Object scan() {
        return success(boxService.scan(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    /**
     * 查询打码top5
     *
     * @return
     */
    @PostMapping("/scanTop")
    public Object scanTop() {
        return success(boxService.scanTop(UserAuthUtil.getLoginUser().getCompanyId()));
    }

    /**
     * 种植系统首页
     * 赋码数量统计,符码数量统计,扫码分布统计
     *
     * @return
     */
    @PostMapping(value = "/codeCount")
    public Object codeCount(@RequestBody JSONObject params) {
        String companyId = params.getString("companyId");
        JSONObject result = new JSONObject();
        if (!StringUtils.isEmpty(companyId)) {
            result.put("fuCodeNum", boxService.fuCode(companyId));    //符码数量统计
            result.put("scanCode", scanRecordService.scanCode(companyId));    //扫码数量统计
            result.put("scanCodeAddress", scanRecordService.scanCodeAddress(companyId));    //扫码分布统计
            result.put("areaNumCount", baseSoilService.areaNumCount(companyId));    //地块数量统计
            return success(result);
        }
        return fail("companyId不能为空");
    }

    //在线打印获取数据
    @GetMapping(value = "/print/{id}")
    public Object print(@PathVariable("id") String id) {
        JSONObject param = new JSONObject();
        Subpackage subpackage = subpackageService.findById(id);
        subpackage.getCompanyId();
        List<String> codeList = boxService.findBySubId(id);
        param.put("print", subpackage);
//        param.put("company","company");
        param.put("codeList", codeList);
        return success(param);
    }

}


